package com.kgc.web;

import com.kgc.dao.AdminDanYuanDao;
import com.kgc.dao.AdminRoomDao;
import com.kgc.entity.Danyuan;
import com.kgc.entity.Room;
import com.kgc.entity.UserRoom;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "RoomServlet", value = "/RoomServlet")
public class RoomServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opt = request.getParameter("opt");
        AdminRoomDao adminRoomDao = new AdminRoomDao();
        PrintWriter writer = response.getWriter();
        PageUtils pageUtils = new PageUtils();
        if("list".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String status = request.getParameter("name");
            int totalRoom = adminRoomDao.totalRoom(status);
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<Room> roomList = adminRoomDao.selectRoom(pageUtils, status);
            pageUtils.init(totalRoom);
            writer.write(R.success("数据加载成功", totalRoom, roomList).toJSON());
        }else if("danyuanlist".equals(opt)){
            AdminDanYuanDao adminDanYuanDao = new AdminDanYuanDao();
            List<Danyuan> list = adminDanYuanDao.selectdanyuan();
            writer.write(R.success("数据加载成功",list).toJSON());
        }else if("add".equals(opt)){
            String name = request.getParameter("name");
            String area = request.getParameter("area");
            String danyuan = request.getParameter("danyuan");
            int i = adminRoomDao.addRoom(new Room(name,area,"0",danyuan));
            if (i > 0) {
                writer.write(R.success("添加成功!").toJSON());
            } else {
                writer.write(R.error("添加失败!").toJSON());
            }
        }else if("delete".equals(opt)){
            String id = request.getParameter("id");
            int count = adminRoomDao.deleteRoom(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        }else if("Edit".equals(opt)){
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String area = request.getParameter("area");
            String danyuan = request.getParameter("danyuan");
            int count = adminRoomDao.updateroom(new Room(Integer.parseInt(id),name,area,Integer.parseInt(danyuan)));
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败").toJSON());
            }
        }else if("uerRoomList".equals(opt)){
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String id = request.getParameter("id");
            int totaluserRoom = adminRoomDao.totaluserRoom(Integer.parseInt(id));
            if (page != null) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (limit != null) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            List<UserRoom> list = adminRoomDao.selectUserRoom(pageUtils, Integer.parseInt(id));
            pageUtils.init(totaluserRoom);
            writer.write(R.success("数据加载成功", totaluserRoom, list).toJSON());
        }else if ("danyuan".equals(opt)) {
            int did = Integer.parseInt(request.getParameter("did"));
            List<Room> roomList = adminRoomDao.selectRoomByDanYuan(did);
            writer.write(R.success("房屋列表加载成功..", roomList).toJSON());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
